package com.wjjun.databinding;

import android.databinding.BindingAdapter;
import android.text.TextUtils;
import android.view.View;

import com.wjjun.customview.views.StateView;
import com.wjjun.presenter.ViewGroupPresenter;
import com.wjjun.viewmodel.StateViewModel;

/**
 * stateView
 * Created by 67535 on 2018/5/7.
 */
public class StateViewAdapter {

    @BindingAdapter(value = {"setStates"})
    public static void stateView(StateView stateView, StateViewModel stateViewModel) {
        if (stateViewModel == null) {
            return;
        }

        if (!TextUtils.isEmpty(stateViewModel.getLoadingText())) {
            stateView.setLoadingText(stateViewModel.getLoadingText());
        }

        if (stateViewModel.getEmptyImg() != 0) {
            stateView.setEmptyImageId(stateViewModel.getEmptyImg());
        }
        if (!TextUtils.isEmpty(stateViewModel.getEmptyText())) {
            stateView.setEmptyText(stateViewModel.getEmptyText());
        }


        if (stateViewModel.getNotNetImg() != 0) {
            stateView.setNoNetworkImageId(stateViewModel.getNotNetImg());
        }
        if (!TextUtils.isEmpty(stateViewModel.getNotNetText())) {
            stateView.setNoNetworkText(stateViewModel.getNotNetText());
        }

        if (stateViewModel.getNetErrorImg() != 0) {
            stateView.setErrorImageId(stateViewModel.getNetErrorImg());
        }
        if (!TextUtils.isEmpty(stateViewModel.getNetErrorText())) {
            stateView.setErrorText(stateViewModel.getNetErrorText());
        }
    }

    @BindingAdapter(value = {"onRefresh"})
    public static void onRefresh(final StateView stateView, final ViewGroupPresenter viewGroupPresenter) {
        if (viewGroupPresenter != null) {
            stateView.setOnRefreshListener(new StateView.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    viewGroupPresenter.onRefresh(null);
                }

                @Override
                public void onClick(View view) {
                    viewGroupPresenter.onClick(view);
                }
            });
        }
    }

    @BindingAdapter(value = {"switchState"})
    public static void switchState(StateView stateView, int state) {
        if (state == StateView.STATE_LOADING) {
            stateView.showLoading();
        } else if (state == StateView.STATE_SUCCESS) {
            stateView.showSuccess();
        } else if (state == StateView.STATE_EMPTY) {
            stateView.showEmpty();
        } else if (state == StateView.STATE_NO_NETWORK) {
            stateView.showNoNetwork();
        } else if (state == StateView.STATE_NETWORK_ERROR) {
            stateView.showError();
        }
    }
}
